package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.QuestionBank;
import com.itheima.service.QuestionBankService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/questionbank")
public class QuestionBankController {
    @Reference
    private QuestionBankService questionBankService;

    @RequestMapping("/add")
    public Result add(@RequestBody QuestionBank questionBank){
        try{
            questionBankService.add(questionBank);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_QUESTIONBANK_FAIL);
        }
        return new Result(true,MessageConstant.ADD_QUESTIONBANK_SUCCESS );
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = questionBankService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody QuestionBank questionBank){
        try{
            questionBankService.edit(questionBank);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_QUESTIONBANK_FAIL );
        }
        return new Result(true, MessageConstant.EDIT_QUESTIONBANK_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            questionBankService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_QUESTIONBANK_FAIL );
        }
        return new Result(true, MessageConstant.DELETE_QUESTIONBANK_SUCCESS);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
           QuestionBank questionBank =  questionBankService.findById(id);
           return new Result(true,MessageConstant.QUERY_QUESTIONBANK_SUCCESS,questionBank );
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_QUESTIONBANK_FAIL );
        }
    }
}
